package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.ChangeShifts2Adapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.ChangeShiftEntity;
import com.glory.brepda.entity.ChangeshifineListEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ShiftExchange2Activity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etShiftNo)
    FreeUI_ClearEditText etShiftNo;
    @BindView(R.id.etWorkNo)
    FreeUI_ClearEditText etWorkNo;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.tvShiftTime)
    TextView tvShiftTime;
    @BindView(R.id.etShift2No)
    FreeUI_ClearEditText etShift2No;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.tvGetData)
    TextView tvGetData;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.list_tv5)
    TextView listTv5;
    @BindView(R.id.list_tv6)
    TextView listTv6;
    @BindView(R.id.list_tv7)
    TextView listTv7;
    @BindView(R.id.rvWorkNoList)
    RecyclerView rvWorkNoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private ChangeShifts2Adapter mChangeShiftsAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_shift_exchange2;
    }

    @Override
    protected void initView() {
        initLayout();
        mChangeShiftsAdapter = new ChangeShifts2Adapter(null);
        mChangeShiftsAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mChangeShiftsAdapter.isFirstOnly(true);
        rvWorkNoList.setAdapter(mChangeShiftsAdapter);

    }

    private void initLayout() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        listTv1.setText("单据号");
        listTv2.setText("交班人工号");
        listTv3.setText("批号");
        listTv4.setText("交班数量");
        listTv5.setText("设备ID");
        listTv6.setText("批次状态");
        listTv7.setText("接班数量");
        listTv5.setVisibility(View.VISIBLE);
        listTv6.setVisibility(View.VISIBLE);
        listTv7.setVisibility(View.VISIBLE);
        etShift2No.setText(Constant.CURRENT_USER);
        initRecyclerView(rvWorkNoList,this);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit,R.id.tvGetData})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
//                if (actionControl("Submit18")){
//                    return;
//                }

                if (getText(etShift2No).equals("")){
                    showToast("接班人工号不能为空",false);
                    return;
                }
                showSubmitDialog(() -> submit("TO",false));
                break;
            case R.id.tvGetData:
                if (getText(etShiftNo).equals("") && getText(etWorkNo).equals("") && getText(etLotID).equals("")){
                    showToast("交班人工号或单据号或批次号，必填一项",false);
                    return;
                }
                mChangeShiftsAdapter.getData().clear();
                mChangeShiftsAdapter.setShiftNo("");
                submit("SELECT",true);
                break;
        }

    }

    private void submit(String actionType,boolean action){
        FreeUI_EntityCallBack<BaseResponseBean<ChangeShiftEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ChangeShiftEntity>>
                (new TypeToken<BaseResponseBean<ChangeShiftEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                    if (!action){
                        showConfirmDialog(ShiftExchange2Activity.this,"接班成功");
                        return;
                    }
                    List<ChangeShiftEntity.ChangeShift> mDataList = response.body().getResponse().getBody().getCHANGESHIFTLINELIST();
                    if (mDataList.size() <= 0){
                        showToast("未查询到交接班信息",false);
                        return;
                    }
                    mChangeShiftsAdapter.setShiftNo(response.body().getResponse().getBody().getREFEROWNER());
                    mChangeShiftsAdapter.setNewData(mDataList);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"CHANGESHIFT");

            }
        };
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sim = dateFormat.format(date);

        JsonObject map = new JsonObject();
//        map.addProperty("ACTIONTYPE",actionType);
//        map.addProperty("REFEROWNER",getText(etShiftNo));
//        map.addProperty("TOOWNER",getText(etShift2No));
//        map.addProperty("DOCID","");
//        map.addProperty("TOTIME","");
//        map.addProperty("REFERTIME","");
//        map.addProperty("COMMENTS",getText(etComment));
//        ChangeshifineListEntity changeshifineListEntity = new ChangeshifineListEntity();
//        List<ChangeshifineListEntity> lotactionList = new ArrayList<>();
//        changeshifineListEntity.setDOCID("");
//        changeshifineListEntity.setTOTIME("");
//        changeshifineListEntity.setREFERTIME("");
//        changeshifineListEntity.setCHANGESHIFTID("");
//        changeshifineListEntity.setCHANGESHIFTRRN(610902);
//        changeshifineListEntity.setTYPE("");
//        changeshifineListEntity.setITEM_ID(getText(etComment));
//        changeshifineListEntity.setRESERVED05("");
//
//        lotactionList.add(changeshifineListEntity);
//                map.add("CHANGESHIFTLINELIST",new JsonParser().parse(new Gson().toJson(mChangeShiftsAdapter.getData())).getAsJsonArray());
//        map.add("CHANGESHIFTLINELIST", new JsonParser().parse(new Gson().toJson(lotactionList)).getAsJsonArray());


        map.addProperty("ACTIONTYPE",actionType);
        map.addProperty("REFEROWNER",getText(etShiftNo));
        map.addProperty("TOREFER",getText(etShift2No));
        map.addProperty("DOCID",getText(etWorkNo));
        if (action){
            map.addProperty("LOTID",getText(etLotID));
        }
        NetUtils.requestPostNet("IF.CHANGESHIFT","Common",ShiftExchange2Activity.this,map,callBack);
    }

}
